/*
 * Copyright (C) 2015 huwentao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mokey.core.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by huwentao on 16/12/3.
 */

public class AppManager {
    private Map<String, BaseActivity> mActivityMap = new HashMap<>();
    private List<String> mActTags = new ArrayList<>();
    private boolean isStart = false;
    private Map<String, Object> tempParams = new HashMap<>();

    private AppManager() {
    }

    public static AppManager get() {
        return Instance.INSTANCE;
    }

    private static class Instance {
        private final static AppManager INSTANCE = new AppManager();
    }

    /**
     *
     */
    public void start() {
        isStart = true;
    }

    /**
     *
     */
    public void endAndFinish() {
        isStart = false;
        for (String tag : mActTags) {
            BaseActivity baseActivity = mActivityMap.get(tag);
            if (baseActivity != null)
                baseActivity.finish();
        }
        mActTags.clear();
    }

    /**
     * @param baseActivity
     */
    public void putActivity(BaseActivity baseActivity) {
        if (baseActivity != null) {
            String TAG = baseActivity.getTag();
            if (isStart) {
                mActTags.add(TAG);
            }
            mActivityMap.put(TAG, baseActivity);
        }
    }

    /**
     * @param actTag
     */
    public void finish(String actTag) {
        BaseActivity baseActivity = mActivityMap.remove(actTag);
        if (baseActivity != null &&
                !baseActivity.isFinishing() &&
                !baseActivity.isDestroyed())
            baseActivity.finish();
    }

    /**
     * @param baseActivity
     */
    public void finish(BaseActivity baseActivity) {
        if (baseActivity != null) {
            if (!baseActivity.isFinishing() && !baseActivity.isDestroyed()) {
                baseActivity.finish();
            }
            finish(baseActivity.getTag());
        }
    }

    /**
     *
     */
    public void finish() {
        isStart = false;
        Set<String> keys = mActivityMap.keySet();
        for (String key : keys) {
            finish(mActivityMap.get(key));
        }
    }

    /**
     *
     */
    public void clear() {
        isStart = false;
        mActTags.clear();
        mActivityMap.clear();
        tempParams.clear();
    }

    /**
     * @param context
     * @param aClass
     */
    public void startActivity(Context context, Class<? extends BaseActivity> aClass) {
        Intent intent = new Intent(context, aClass);
        context.startActivity(intent);
    }

    /**
     * @param context
     * @param aClass
     * @param bundle
     */
    public void startActivity(Context context, Class<? extends BaseActivity> aClass, Bundle bundle) {
        Intent intent = new Intent(context, aClass);
        if (bundle != null)
            intent.putExtras(bundle);
        context.startActivity(intent);
    }

    /**
     * @param context
     * @param aClass
     * @param requestCode
     */
    public void startActivityForResult(Activity context, Class<? extends BaseActivity> aClass, int requestCode) {
        Intent intent = new Intent(context, aClass);
        context.startActivityForResult(intent, requestCode);
    }

    /**
     * @param context
     * @param aClass
     * @param requestCode
     * @param bundle
     */
    public void startActivityForResult(Activity context, Class<? extends BaseActivity> aClass, int requestCode, Bundle bundle) {
        Intent intent = new Intent(context, aClass);
        if (bundle != null)
            intent.putExtras(bundle);
        context.startActivityForResult(intent, requestCode);
    }

    /**
     * @param tempParam
     */
    public void setTempParam(Object tempParam) {
        if (tempParam != null) {
            Class<?> aClass = tempParam.getClass();
            tempParams.put(aClass.getName() + aClass.hashCode(), tempParam);
        }
    }

    /**
     * @param aClass
     * @param <T>
     * @return
     */
    public <T> T getTempParam(Class<T> aClass) {
        Object o = tempParams.get(aClass.getName() + aClass.hashCode());
        if (o != null && o.getClass().isAssignableFrom(aClass)) {
            return aClass.cast(o);
        }
        return null;
    }

    /**
     * @param key
     * @param tempParam
     */
    public void setTempParam(String key, Object tempParam) {
        if (tempParam != null) {
            if (TextUtils.isEmpty(key)) {
                setTempParam(tempParam);
            } else {
                tempParams.put(key, tempParam);
            }
        }
    }

    /**
     * @param key
     * @return
     */
    public Object getTempParam(String key) {
        if (!TextUtils.isEmpty(key)) {
            return tempParams.get(key);
        }
        return null;
    }

    /**
     * @param key
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> T getTempParam(String key, Class<T> tClass) {
        Object o = getTempParam(key);
        if (o != null && o.getClass().isAssignableFrom(tClass)) {
            return tClass.cast(o);
        }
        return null;
    }

    /**打开网站
     * @param context
     * @param webSite
     */
    public void openWebSite(Activity context, String webSite) {
        if (context == null || !TextUtils.isEmpty(webSite)||!TextUtils.isEmpty(webSite.trim())) {
            return;
        }

        if (! webSite.startsWith("http://") && ! webSite.startsWith("https://")) {
            webSite = "http://" + webSite;}
        final Uri uri = Uri.parse(webSite);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        context.startActivity(intent);
    }
}
